package com.sensePlusVR.fragment;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sensePlusVR.adapter.CollectionListAdapter;
import com.sensePlusVR.adapter.CollectionListAdapter.OnCollectionDownloadedListener;
import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.base.BaseFragment;
import com.sensePlusVR.bean.CollectFollowIds;
import com.sensePlusVR.bean.CollectionItem;
import com.sensePlusVR.bean.EventBusBean;
import com.sensePlusVR.bean.GameItem;
import com.sensePlusVR.main.R;
import com.sensePlusVR.manager.DataCenter;
import com.sensePlusVR.manager.EventListener;
import com.sensePlusVR.manager.GameItemManager;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.DialogUtils;
import com.sensePlusVR.utils.EventBusUtils;
import com.sensePlusVR.utils.GameDownload;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.NetWorkNotify;
import com.sensePlusVR.utils.PageUtil;
import com.sensePlusVR.utils.UserUtil;
import com.sensePlusVR.widget.FullyLinearLayoutManager;
import com.sensePlusVR.widget.sliddel.ItemRemoveRecyclerView;
import com.sensePlusVR.widget.sliddel.OnItemClickListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author: Loren
 * @create: 2016-7-26下午1:36:06
 * @desc: 游戏展示 TODO : Item之间的间距 jsonobject缓存使用ASimpleCache ,jsonobject key
 *        使用拼接的完整的URL
 */
@SuppressLint({ "UseSparseArrays", "NewApi" })
@SuppressWarnings({ "unused"})
@ContentView(R.layout.fragment_collection)
public class CollectionFragment extends BaseFragment{
	private static final String TAG = CollectionFragment.class.getSimpleName();
	private static final String EVENT_KEY=CollectionFragment.class.getSimpleName();
	private List<GameItem> gameDownedList;
	private List<GameItem> gameCollectionList;
	private CollectionListAdapter downloadAdapter;
	private CollectionListAdapter collecitonAdapter;
	private DataCenter dataCenter;
	
	@ViewInject(R.id.downloaded_pull_down_iv)
	private ImageView downloaded_pull_down_iv;
	
	@ViewInject(R.id.downloaded_gridview)
	private ItemRemoveRecyclerView downloaded_gridview;
	
	@ViewInject(R.id.collection_pull_down_iv)
	private ImageView collection_pull_down_iv;
	
	@ViewInject(R.id.collection_gridview)
	private ItemRemoveRecyclerView collection_gridview;
	
	@ViewInject(R.id.collection_network_tip_relative)
	private RelativeLayout collection_network_tip_relative;
	
	@ViewInject(R.id.collection_network_tip_tv)
	private TextView collection_network_tip_tv;
	
	
	private boolean isChecked = false;//是否选中标记

	private int pageSize = PageUtil.GAME_PAGE_SIZE;
	private int currentPage = 1;
	
	private boolean isUpdate = false;
	
	private VRApplication vrApplication;
	
	private int userId;
	
	private final String KEY = this.getClass().getSimpleName();
	
	private View rootView;
	
	private View curItemView;
	
	private boolean isOpen = false;//标记play button是否展开
	
//	private Map<Integer, CommendItem> commendMap;
	
	
	EventListener listenerGameItem = new EventListener(){
		@Override
		public void Notify(int event, Object data, Object Params) {
			if (Params == null || data == null)
				return;
			MyLog.d(TAG, "game Event="+event);
			//当EventGameItemRefreshGot时候也可能已存在Item中字段需要更新(比如名称修改),所以在此处刷新界面显示最新的数据
			if (event == GameItemManager.EventGameItemIdsGot || event == GameItemManager.EventGameItemRefreshGot){
				updateCollectionedGame();
			}
		}
	};
	
	void updateCollectionedGame() {
		((BaseActivity) getActivity()).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				userId = UserUtil.getUserId();
				CollectFollowIds cfIds = dataCenter.getCollectFollowIds(userId);
				if (cfIds != null) {
					MyLog.d(TAG, "cfIds : " + cfIds.toString());
					// 因为第一次或者退出后本地数据库清空了，clcItems为空，只有在此处回调后才有值做处理
					List<CollectionItem> clcItems = CollectionItem
							.getAllCollectionItems();
					if (clcItems == null) {
						MyLog.d(TAG, "clcItems == null");
						ArrayList<Integer> colleIds = cfIds.getCollectionList();
						if (colleIds == null)
							return;
						MyLog.d(TAG, "colleIds : " + colleIds.toString());
						// 如果有收藏并且clcItems == null 则把收藏数据添加到CollcetionItem中
						List<GameItem> list = GameItem
								.getGameListByIds(colleIds);
						if (list != null) {
							MyLog.d(TAG, "gameItem form DB:" + list.size());
							for (GameItem gameItem : list) {
								CollectionItem.addCollectionItem(gameItem);
							}
						}
						clcItems = CollectionItem.getAllCollectionItems();
					}

					if (clcItems != null) {
						MyLog.d(TAG, "clcItems size : " + clcItems.size());
						// Collections.reverse(clcItems);
						if (gameDownedList == null)
							gameDownedList = new ArrayList<GameItem>();
						else
							gameDownedList.clear();
						if (gameCollectionList == null)
							gameCollectionList = new ArrayList<GameItem>();
						else
							gameCollectionList.clear();
						
						for(CollectionItem clcItem : clcItems){
							
							GameItem gameItem = GameItem.getGameById(clcItem.getGameId());;
							
							if(clcItem.getDownloadState()==CollectionItem.DownloadFinished){
								if(gameItem != null){
									gameDownedList.add(gameItem);
								}
							}else{
								if(gameItem != null){
									MyLog.d(TAG, "gameCollectionList add gameItem : " + gameItem);
									gameCollectionList.add(gameItem);
								}
							}
						}
					}
				}
				
				MyLog.d(TAG, "gameDownedList size : " + gameDownedList.size() + " gameCollectionList size : " + gameCollectionList.size());
				
				if (downloadAdapter != null)
					downloadAdapter.refreshData(gameDownedList);
				
				if (collecitonAdapter != null)
					collecitonAdapter.refreshData(gameCollectionList);
			}
		});
	}
	
	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		dataCenter = DataCenter.getInstans();
		vrApplication = (VRApplication) getActivity().getApplication();
		//请求搜藏数据库
		
		//变量初始化
		gameDownedList = new ArrayList<GameItem>();
		gameCollectionList = new ArrayList<GameItem>();
		
//		commendMap = new HashMap<Integer, CommendItem>();
		
		downloadAdapter = new CollectionListAdapter(getActivity(),gameDownedList,Constant.FROM_DOWNLOADED);
		
		collecitonAdapter = new CollectionListAdapter(getActivity(),gameCollectionList,Constant.FROM_COLLECTION);
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (rootView == null) {
			rootView = x.view().inject(this, inflater, container);
			initView();
		}

		return rootView;
	}

	private void initView() {
		//已下载
		FullyLinearLayoutManager layoutDownloaded = new FullyLinearLayoutManager(getActivity());
		layoutDownloaded.setSmoothScrollbarEnabled(true);
		layoutDownloaded.setAutoMeasureEnabled(true);
		downloaded_gridview.setLayoutManager(layoutDownloaded);
		downloaded_gridview.setNestedScrollingEnabled(false);
		downloaded_gridview.setAdapter(downloadAdapter);

		//未下载
		FullyLinearLayoutManager layoutCollection = new FullyLinearLayoutManager(getActivity());
		layoutCollection.setSmoothScrollbarEnabled(true);
		layoutCollection.setAutoMeasureEnabled(true);
		collection_gridview.setLayoutManager(layoutCollection);
		collection_gridview.setNestedScrollingEnabled(false);
		collection_gridview.setAdapter(collecitonAdapter);
	}

	/**
	 * 初次获取网络状态
	 */
	private void initNetworkState() {
		int status = GameDownload.getDownloadStauts();
		notifyNetworkUI(status);
	}
	
	private NetWorkNotify netWorkNotify = new NetWorkNotify() {
		
		@Override
		public void notify(int status) {
			
			notifyNetworkUI(status);
				
		}

	};
	
	private void notifyNetworkUI(int status) {
		MyLog.d(TAG, "notifyNetworkUI status : " + status);
		switch (status) {	
		case GameDownload.GameDownLoadByWifi:
			setNetworkUI(false, Constant.NETWORK_TYPE_NULL);
			break;
		case GameDownload.GameDownLoadByCelluar:
			setNetworkUI(true, Constant.NETWORK_TYPE_CONNECT_WIFI);
			break;
		case GameDownload.GameDownLoadNotAllowCelluar:
			setNetworkUI(true, Constant.NETWORK_TYPE_CONNECT_WIFI);
			break;
		case GameDownload.GameDownLoadNoNetwork:
			setNetworkUI(true, Constant.NETWORK_TYPE_CONNECT_NETWORK);
			break;
		case GameDownload.GameDownLoadItemExsit:
			break;
		case GameDownload.GameDownLoadItemNULL:			
			break;
		}
	}
	
	
	private void setNetworkUI(boolean visible,int tipType){
		MyLog.d(TAG, "setNetworkUI visible : " + visible);
		//只有当下载队列中有下载任务才会显示提示
		int downloadTaskSize = GameDownload.getDownloadTaskSize();
		MyLog.d(TAG, "setNetworkUI downloadTaskSize : " + downloadTaskSize);
		if(downloadTaskSize == 0){
			collection_network_tip_relative.setVisibility(View.GONE);
			return;
		}
		
		if(visible){
			collection_network_tip_relative.setVisibility(View.VISIBLE);
			int textTip = 0;
			int textLeftDrawable = 0;
			
			if(tipType == Constant.NETWORK_TYPE_CONNECT_WIFI){
				
				textTip = R.string.wifi_no_cellular_yes_remain_tip;
				textLeftDrawable = R.drawable.favorite_wifi_icon;
				
				
			}else if(tipType == Constant.NETWORK_TYPE_CONNECT_NETWORK){
				
				textTip = R.string.wifi_no_cellular_no_remain_tip;
				textLeftDrawable = R.drawable.favorite_attention_icon;
				
			}else{
				return;
			}
			
			collection_network_tip_tv.setText(textTip);
			Drawable leftDrawable = ContextCompat.getDrawable(getActivity(), textLeftDrawable);
			collection_network_tip_tv.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null);
			
		}else{
			collection_network_tip_relative.setVisibility(View.GONE);
		}
	}
	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		if (rootView != null) {
			((ViewGroup) rootView.getParent()).removeView(rootView);
		}
	}
	
	
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if(!isVisibleToUser){
			onCollcetionItemClickListener.recoveryDrawer();
			onDownloadItemClickListener.recoveryDrawer();
		}
	}
	
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
	}
	
	

	
	
	private void refreshData() {
	
		
		//服务器请求数据
		userId = UserUtil.getUserId();
		
		MyLog.d(TAG, "userId : " + userId);
		
		CollectFollowIds cfIds = dataCenter.getCollectFollowIds(userId);

//		commendMap.clear();
		
		if(cfIds!=null){
			
			MyLog.d(TAG, "cfIds : " + cfIds.toString());
			
			List<CollectionItem> clcItems = CollectionItem.getAllCollectionItems();
			
			if (clcItems == null) {
				
				MyLog.d(TAG, "clcItems == null");
				
				ArrayList<Integer> colleIds = cfIds.getCollectionList();
				
				if(colleIds==null)return;
				
				MyLog.d(TAG, "colleIds : " + colleIds.toString());
				
				// 如果有收藏并且clcItems == null 则把收藏数据添加到CollcetionItem中
				List<GameItem> list = GameItem.getGameListByIds(colleIds);
				if (list != null) {
					MyLog.d(TAG, "gameItem form DB:" + list.size());

					for (GameItem gameItem : list) {

						CollectionItem.addCollectionItem(gameItem);

					}

				}
				clcItems = CollectionItem.getAllCollectionItems();
			}

			List<Integer> itsDownloaded = new ArrayList<Integer>();
			List<Integer> itsNotDownloaded = new ArrayList<Integer>();
			
			if(clcItems != null){
				
				MyLog.d(TAG, "clcItems size is "+clcItems.size());
				for(CollectionItem clcItem:clcItems){
					
					if(clcItem.getDownloadState()==CollectionItem.DownloadFinished){
						
						itsDownloaded.add(clcItem.getGameId());
					}else{
						itsNotDownloaded.add(clcItem.getGameId());
					}
					
//					CommendItem citem = dataCenter.getGameCommend(clcItem.getGameId());
//					if (citem != null) {
//
//						commendMap.put(clcItem.getGameId(), citem);
//					}
					
					MyLog.d(TAG, "collection game id = "+clcItem.getGameId());
				}	
			}
			
			int downCount = itsDownloaded.size();
			
			int unDownCount = itsNotDownloaded.size();
			
			MyLog.d(TAG, "downCount="+downCount+" unDownCount="+unDownCount);
				
			gameDownedList = GameItem.getGameListByIds(itsDownloaded);	
			gameCollectionList = GameItem.getGameListByIds(itsNotDownloaded);
			
		}else{
			
			MyLog.d(TAG, "cfIds size is null");
			
		}
		
		//由于从本地数据库中有可能获取空数据
		if(gameDownedList == null)
		gameDownedList = new ArrayList<GameItem>();
		if(gameCollectionList == null)
		gameCollectionList = new ArrayList<GameItem>();
		
		//Collections.reverse(gameDownedList);
		//Collections.reverse(gameCollectionList);
		
		int downGameCount = gameDownedList!=null?gameDownedList.size():0;
		int unDownGameCount = gameCollectionList!=null?gameCollectionList.size():0;
		
		MyLog.d(TAG, "downGameCount="+downGameCount+" unGameDownCount="+unDownGameCount);
		
//		downloadAdapter.setCommendMap(commendMap);
//		collecitonAdapter.setCommendMap(commendMap);
		
		downloadAdapter.refreshData(gameDownedList);
			
		collecitonAdapter.refreshData(gameCollectionList);
	}
	
	
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onMessageEvent(EventBusBean event){
		String eventMessage = event.getMessage();
		MyLog.d(TAG, "onMessageEvent : " + eventMessage);
		if(eventMessage.equals(EventBusUtils.EVENT_NOTIFY_COLLECTION_FRAGMENT)){
			
			updateCollectionedGame();
			
		}else if(eventMessage.equals(EventBusUtils.EVENT_COLLECTION_ONBINDVIEWHOLDER)){
			
			initNetworkState();
			
		}
		
	}


	
	/**
	 * Downloaded侧滑监听
	 */
	OnItemClickListener onDownloadItemClickListener = new OnItemClickListener(){
		
		private int x;
		private int y;
		
		@Override
		public void onItemClick(View view, int position) {}

		@Override
		public void onDeleteClick(int position) {
			MyLog.d(TAG, "onDeleteClick onDownloadItemClickListener position : " + position);
			if(gameDownedList == null)return;
			if(position >= 0 && position < gameDownedList.size()){
				GameItem gameItem = gameDownedList.get(position);
				if(gameItem == null)return;
				deleteDownloadORConnection(gameItem,Constant.FROM_DOWNLOADED);
			}
		}

		@Override
		public void onTopClick(int position) {
			MyLog.d(TAG, "onTopClick onDownloadItemClickListener position : " + position);
			if(gameDownedList == null)return;
			if(position >= 0 && position < gameDownedList.size()){
				GameItem gameItemCur = gameDownedList.get(position);
				GameItem gameItemFirst = gameDownedList.get(0);
				if(gameItemCur == null)return;
				downloadAdapter.move(gameItemCur);
				//更改数据库置顶状态
				CollectionItem clcItemCur = CollectionItem.query(gameItemCur.getId());
				CollectionItem clcItemFirst = CollectionItem.query(gameItemFirst.getId());
				if(clcItemCur == null || clcItemFirst == null)return;
				long curTopCount = clcItemCur.getTopSort() + 1 + clcItemFirst.getTopSort();
				clcItemCur.setTopSort(curTopCount);
				CollectionItem.saveOrUpdate(clcItemCur);
			}
		}

		@Override
		public void onDeleteState(int x,int y) {
			
			this.x = x;
			this.y = y;
			
			if(collection_gridview == null)return;
			
			if(collection_gridview.getmDeleteBtnState() == ItemRemoveRecyclerView.BTN_OPENED){
				
				downloaded_gridview.recoveryClick(x, y);
				
				collection_gridview.deleteBtnToClosed();
			}
		}

		@Override
		public void recoveryDrawer() {
			onDeleteState(x, y);
		}
		
	};
	
	/**
	 * Collection侧滑监听
	 */
	OnItemClickListener onCollcetionItemClickListener = new OnItemClickListener(){
		
		private int x;
		private int y;

		@Override
		public void onItemClick(View view, final int position) {
			MyLog.d(TAG, "onItemClick onCollcetionItemClickListener position : " + position);
		}

		@Override
		public void onDeleteClick(int position) {
			MyLog.d(TAG, "onDeleteClick onCollcetionItemClickListener position : " + position);
			if(gameCollectionList == null)return;
			if(position >= 0 && position < gameCollectionList.size()){
				GameItem gameItem = gameCollectionList.get(position);
				if(gameItem == null)return;
				deleteDownloadORConnection(gameItem,Constant.FROM_COLLECTION);
				
				
			}
		}

		@Override
		public void onTopClick(int position) {}

		@Override
		public void onDeleteState(int x,int y) {
			
			this.x = x;
			this.y = y;
			
			if(downloaded_gridview == null)return;
			
			if(downloaded_gridview.getmDeleteBtnState() == ItemRemoveRecyclerView.BTN_OPENED){
				
				
				collection_gridview.recoveryClick(x, y);
				
				downloaded_gridview.deleteBtnToClosed();
			}
		}
		
		@Override
		public void recoveryDrawer() {
			onDeleteState(x, y);
		}
	};
	
	

	
	public OnCollectionDownloadedListener onCollectionDownloadedListener = new OnCollectionDownloadedListener(){

		@Override
		public void onCollectionDownloaded(GameItem gameItem) {
			MyLog.d(TAG, "onCollectionDownloaded  gameItem : " + gameItem.toString());
			collecitonAdapter.remove(gameItem);
			boolean isAdd = false;
			if(gameDownedList.size()== 0){
				isAdd = true;
			}else{
				for(GameItem gItem : gameDownedList){
					if(gItem.getId() != gameItem.getId()){
						isAdd = true;
					}else{
						isAdd = false;
						break;
					}
				}	
			}
			if(isAdd){
				downloadAdapter.add(gameItem, 0);
			}
		}
	};
	
	@Event(value = {R.id.downloaded_pull_down_relative,R.id.collection_pull_down_relative})
	private void setViewOnClick(View view){
		switch (view.getId()) {
		case R.id.downloaded_pull_down_relative:
			MyLog.d(TAG, "downloaded_pull_down_relative");
			if(downloaded_gridview.getVisibility() == View.VISIBLE){
				downloaded_gridview.setVisibility(View.GONE);
				downloaded_pull_down_iv.setImageResource(R.drawable.list_unfold_selector);
			}else{
				downloaded_gridview.setVisibility(View.VISIBLE);
				downloaded_pull_down_iv.setImageResource(R.drawable.list_fold_selector);
			}
			break;
		case R.id.collection_pull_down_relative:
			MyLog.d(TAG, "collection_pull_down_relative");
			if(collection_gridview.getVisibility() == View.VISIBLE){
				collection_gridview.setVisibility(View.GONE);
				collection_pull_down_iv.setImageResource(R.drawable.list_unfold_selector);
			}else{
				collection_gridview.setVisibility(View.VISIBLE);
				collection_pull_down_iv.setImageResource(R.drawable.list_fold_selector);
			}
			break;
		}
	}
	
	
	public void deleteDownloadORConnection(final GameItem gameItem,int from){
		
		CollectionItem clcItem = CollectionItem.query(gameItem.getId());
		
		if(clcItem!=null){
			
			dialogDelete(gameItem, clcItem,from);
		}

	}

	/**
	 * 提示不同对话框
	 * @param gameItem
	 *
	 */
	private void dialogDelete(final GameItem gameItem,final CollectionItem collectionItem,final int from) {
		String message = "";
		if(collectionItem.getDownloadState()==CollectionItem.DownloadNone){
			message = getActivity().getString(R.string.collection_delete);
		}else{
			
			 message = getActivity().getString(R.string.download_delete);
		}
		DialogUtils.showConfirmDialogConfirmCancel(getActivity(), null,
				message, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						//正在下载
						//if(collectionItem.getDownloadState()==CollectionItem.DownloadDoing){
							//从task中去除相应Item
 							GameDownload.cancelDownload(gameItem,collectionItem);	
						//}
						
						if(collectionItem.getDownloadState() == CollectionItem.DownloadFinished){
							MyLog.d(TAG, "downloaded remove");
							downloadAdapter.remove(gameItem);
						}else{
							MyLog.d(TAG, "collection remove");
							collecitonAdapter.remove(gameItem);
						}
						
						//取消搜藏
						CollectionItem.cancelCollection(gameItem,true);
						
					 //恢复删除的Item侧滑view,若不恢复则侧滑删除下一个Item时候则第一次滑动为恢复上次的，下一次滑动则才视为滑动当前的Item
						if(from == Constant.FROM_DOWNLOADED){
							downloaded_gridview.startRecovery();
						}else if(from == Constant.FROM_COLLECTION){
							collection_gridview.startRecovery();
						}
						
					}
				}, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						if(from == Constant.FROM_DOWNLOADED){
							downloaded_gridview.setmDeleteBtnState(3);
						}else if(from == Constant.FROM_COLLECTION){
							collection_gridview.setmDeleteBtnState(3);
						}
						dialog.dismiss();
						
					}
				});
	}
	
	
	

	@Override
	protected void onInvisible() {
		super.onInvisible();
	
	}
	
	@Override
	protected void onVisible() {
		super.onVisible();
		
	}
	
	/**
	 * 取消收藏
	 */
	private void cancelCollection(GameItem gameItem) {
		dataCenter.CollectionGameDel(gameItem.getId());
	}
	
	private List<GameItem> getDownloadingList(){
		
		return null;
	}
	
	
	@Override
	public void onResume() {
		super.onResume();
		
		EventBus.getDefault().register(this);
		
		dataCenter.addGameEventListener(listenerGameItem, this, EVENT_KEY);	
		
		collecitonAdapter.setOnCollectionDownloadedListener(onCollectionDownloadedListener);
		
		downloaded_gridview.setOnItemClickListener(onDownloadItemClickListener);
		
		collection_gridview.setOnItemClickListener(onCollcetionItemClickListener);
		
		//设置网络回到监听
		GameDownload.setNetWorkNotify(KEY, netWorkNotify);
		initNetworkState();
	
		
		if(downloadAdapter!=null){
			
			downloadAdapter.onResume();
		}
		
		if(collecitonAdapter!=null){
			
			collecitonAdapter.onResume();
		}

		refreshData();

	}

	@Override
	public void onPause() {
		super.onPause();
		
		EventBus.getDefault().unregister(this);
		
		dataCenter.addGameEventListener(null, this, EVENT_KEY);
		
		collecitonAdapter.setOnCollectionDownloadedListener(null);
		
		downloaded_gridview.setOnItemClickListener(null);
		
		collection_gridview.setOnItemClickListener(null);
		
		GameDownload.setNetWorkNotify(KEY, null);
		
		if(downloadAdapter!=null){
			
			downloadAdapter.onPause();
		}
		
		if(collecitonAdapter!=null){
			
			collecitonAdapter.onPause();
		}

	}
	


}
